package com.leenleda.ips.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.leenleda.ips.BaseActivity;
import com.leenleda.ips.activity.ImageActivity;
import com.leenleda.ips.activity.LiveActivity;
import com.leenleda.ips.activity.WebActivity;
import com.leenleda.ips.entity.DataEntity;
import com.leenleda.ips.net.NetInterface;
import com.leenleda.ips.util.FileData;
import com.leenleda.ips.util.FileUtils;
import com.leenleda.ips.util.LogUtils;
import com.leenleda.ips.util.NetUtils;
import com.leenleda.ips.util.UShared;
import com.leenleda.ips.util.Utils;


public class TimeReceiver extends BroadcastReceiver {
    private static int netConnect = 0;
    private String tag = "TimeReceiver";

    public static int connectNum = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Bundle bundle = null;
        boolean toTool = false;
        int state;
        DataEntity info = FileData.getDeviceInfo(4);
//        ILog.e("SLKVNSDV", "=====TimeReceiver======" + action);
        ReceiverMsg.sendBroad(context);
        switch (action) {
            case Constant.SWITCH_PLAY:
                bundle = intent.getExtras();
                toTool = bundle.getBoolean("toTool");
                UShared.putBooleanValue("toTool", toTool);
            case "android.intent.action.TIME_TICK":
                Constant.NET_STATE = NetUtils.isNetworkAvailable(context);
                if (Constant.NET_STATE){
                    if (connectNum < 3){
                        connectNum ++;
                        if (BaseActivity.getForegroundActivity() != null)
                            BaseActivity.getForegroundActivity().initBase();
                    }
                } else {
                    connectNum = 0;
                }

                long timeOpen = UShared.getLong(Constant.DEVICE_OPEN_TIME, 0);
                long timeFrom = System.currentTimeMillis() / 1000 - timeOpen;
                if (BaseActivity.getForegroundActivity() != null)
                    if (BaseActivity.getForegroundActivity() instanceof ImageActivity){
                        LogUtils.e(tag, "=====timeFrom===1===" + timeFrom + ", timeOpen= " + timeOpen);
                        if (timeFrom > 300 && timeFrom < 422){
                            BaseActivity.getForegroundActivity().conTrollShow(Constant.RELOAD_OPEN_TIME, "");
                        }
                    }

                if (!Constant.NET_STATE) {
                    netConnect ++;
                    boolean isStatic = UShared.getBooleanValue(Constant.LOAD_STATIC_URL);
                    LogUtils.e(tag, "=====timeFrom===netConnect===" + netConnect + ",isStatic=" + isStatic);
                    if (netConnect > 2){
                        netConnect = 0;
                        if (isStatic){
                        } else {
                            restart(context);
                        }
                        return;
                    }
                } else {
                    netConnect = 0;
                    if (Constant.NET_STATE && Constant.DIALOG_IS_SHOW) {//设备有网，同时认证未成功时连接
                        String ipServer = Utils.getStringValue(UShared.getString(Constant.PUBLISH_IP));
                        if (BaseActivity.getForegroundActivity() != null)
                            BaseActivity.getForegroundActivity().connectSer(ipServer);
                    }
                    NetInterface.checkTool();//检查更新工具是否有关闭
                }
                toTool = UShared.getBooleanValue("toTool");
                LogUtils.e(tag, "=====APP_TEST=" + Constant.APP_RESTART + "==toTool="+ toTool + "==info==" + info );
                if (toTool) return;
                boolean isForeground = Utils.isOnForeground(context);
                LogUtils.e(tag, "==isForeground=" + isForeground );
                if (!isForeground)
                    startSelf(context, info);
                break;
            case Constant.TIMER_LOOP_ACTION:
                bundle = intent.getExtras();
                state = bundle.getInt("state");
                String data = bundle.getString("data");
                if (BaseActivity.getForegroundActivity() != null)
                    BaseActivity.getForegroundActivity().conTrollShow(state, data);
                break;
        }
    }

    private void restart(Context context){
        Intent intent1 = new Intent("android.intent.action.reboot");
        context.sendBroadcast(intent1);
    }

    private void startSelf(Context context, DataEntity info, String ... data){
        Log.e(tag, "==信发time==" + Constant.APP_RESTART + "=startSelf======" + info);
        if (!Constant.APP_RESTART){
            return;
        }
        Class toClass = null;
        if (info != null){
            switch (info.getMediaType()){
                case Constant.PUBLISH_SOURCE:
                    toClass = ImageActivity.class;
                    break;
                case Constant.PUBLISH_LIVE:
                    toClass = LiveActivity.class;
                    break;
                case Constant.PUBLISH_WEB:
                default:
                    toClass = WebActivity.class;
                    break;
            }
            Log.e(tag, "==ips=1==toClass======" + toClass + "==getMediaType==" + info.getMediaType());
        } else {
            toClass = WebActivity.class;
        }
        //启动当前主界面
        Intent i = new Intent(context, toClass);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (data.length > 0) {
            UShared.putString(Constant.SET_INPUT_IP, data[0]);
        }
        context.startActivity(i);
    }

}
